package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Set;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> list = employeeMapper.selectForList(qo);
        return new PageInfo<Employee>(list);
    }


    @Override
    public void batchDelete(Long[] ids) {
        if(ids!=null)
        {
            employeeMapper.batchDelete(ids);
        }
    }

    @Override
    public HSSFWorkbook exportExcel() {

        HSSFWorkbook book = new HSSFWorkbook();
        HSSFSheet sheet = book.createSheet("员工信息");
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        List<Employee> list= employeeMapper.selectAll();
        for (int i = 0; i < list.size(); i++) {
            HSSFRow row1 = sheet.createRow(i+1);
            row1.createCell(0).setCellValue(list.get(i).getName());
            row1.createCell(1).setCellValue(list.get(i).getEmail());
            row1.createCell(2).setCellValue(list.get(i).getAge());
        }
        return book;
    }

    @Override
    public void importXls(MultipartFile file) {
        //获取文件  转成对象  插入到数据库

        try {
            HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
            HSSFSheet sheet = workbook.getSheet("员工信息");
            int rowNum = sheet.getLastRowNum();
            for (int i = 1; i <= rowNum; i++) {
                HSSFRow row = sheet.getRow(i);
                String name = row.getCell(0).getStringCellValue();
                String email = row.getCell(1).getStringCellValue();
                Double value = row.getCell(2).getNumericCellValue();
                int age = value.intValue();
                Employee employee = new Employee();
                employee.setName(name);
                employee.setEmail(email);
                employee.setAge(age);
                //添加到数据库
                //先对密码加密 不然无法登陆 默认密码为1
                employee.setPassword(new Md5Hash("1",name).toString());
                employeeMapper.insert(employee);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<Employee> listSellersByRoleSns(String...sns) {
        return employeeMapper.listSellersByRoleSns(sns);
    }

    public void saveOrUpdate(Employee entity, Long[] ids) {
        if (entity.getId() == null) {
            //对密码加密 把Md5Hash 转成字符串
            entity.setPassword((new Md5Hash(entity.getPassword(),entity.getName())).toString());
            employeeMapper.insert(entity);
        } else {
            //删除旧的关系
            employeeMapper.deleteRelation(entity.getId());
            employeeMapper.updateByPrimaryKey(entity);
        }
        //同步保存关系
        if (ids != null) {
            for (Long roleId : ids) {
               employeeMapper.insertRelation(entity.getId(),  roleId);
            }
        }
    }

    public void delete(Long id) {
        if (id != null) {
            //先删除关系,再删除员工
            employeeMapper.deleteRelation(id);
            employeeMapper.deleteByPrimaryKey(id);
        }
    }

    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }
}
